#!/bin/bash
# 使用时注意修改IP地址

netrange=$1
if [ -z $netrange ]
then
  read -p "please set netrange:" netrange
  if [ -z $netrange ]
  then
    echo "no netrange, exit."
    exit
  fi
  echo "netrange:$netrange"
fi

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
getenforce

systemctl stop firewalld
systemctl disable firewalld
systemctl stop NetworkManager
systemctl disable NetworkManager

yum -y install cobbler cobbler-web dhcp tftp-server pykickstart httpd
systemctl start httpd.service cobblerd.service
systemctl enable httpd.service cobblerd.service
cobbler check
#修改cobbler相关参数
#cobbler settings
cp /etc/cobbler/settings{,.bak}
sed -i "s/server: 127.0.0.1/server: $netrange.254/" /etc/cobbler/settings
sed -i "s/next_server: 127.0.0.1/next_server: $netrange.254/" /etc/cobbler/settings
#管理dhcp
sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
#防止重装
sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings
#修改安装完成后的root密码
sed -ri "/default_password_crypted/s#(.*: ).*#\1\"`openssl passwd -1 -salt 'xccsise' 'innobase'`\"#" /etc/cobbler/settings

#启动rsync服务
systemctl start rsyncd
systemctl enable rsyncd
#tftp
cp /etc/xinetd.d/tftp{,.bak}
sed -i 's#yes#no#' /etc/xinetd.d/tftp
systemctl enable tftp.socket
systemctl start tftp.socket
#启动dhcpd服务
systemctl start dhcpd
systemctl enable dhcpd
#电源管理
yum -y install fence-agents

systemctl restart httpd
#下载包所需的软件包
cobbler get-loaders
systemctl restart cobblerd.service
#修改dhcp模板
cp /etc/cobbler/dhcp.template{,.bak}
sed -i "s#192.168.1#$netrange#g" /etc/cobbler/dhcp.template
sed -i "s#192.168.100.254#$netrange.200#g" /etc/cobbler/dhcp.template

#使修改生效
cobbler sync
cobbler check